<template>
	<view class="page">
		<view class="top">
			<view class="background"></view>
		</view>
		<view class="user-card">
			<view class="card">
				<view class="top">
					<view class="userImage" @click="chooseAvatar">
						<u-image v-if="avatar" width="100%" height="100%" shape="circle" mode="aspectFill"
							:src="avatar">
						</u-image>
						<u-image v-else width="100%" height="100%" shape="circle" mode="aspectFill"
							src="https://img0.baidu.com/it/u=705667404,1537851692&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500">
						</u-image>
					</view>
				</view>
				<view class="bottom">
					<view class="left">
						<view class="user-text">
							<text>{{vuex_user.username}}</text>
						</view>
						<view class="user-phone">{{vuex_user.phone}}</view>
					</view>
					<view class="right flex-center">
						<!-- <u-icon name="man" v-if="vuex_user.sex===1"></u-icon>
						<u-icon name="woman" v-else></u-icon> -->
					</view>
				</view>
			</view>
		</view>

		<view class="list-card">
			<view class="card">
				<view class="item item-bottom-solid" @click="toMine">
					<view class="left flex-center">
						<u-icon name="man-add"></u-icon>
					</view>
					<view class="center">
						<text>个人信息</text>
					</view>
					<view class="right flex-center">
						<u-icon class="icon" name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
			<view class="card">
				<view class="item" @click="contactUs">
					<view class="left flex-center">
						<u-icon name="phone"></u-icon>
					</view>
					<view class="center">
						<text>联系我们</text>
					</view>
					<view class="right flex-center">
						<u-icon class="icon" name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<view class="question-list">
			<view class="question-title">回答问卷</view>
			<view class="list-item" v-for="(item,index) in questions" :key="item.id">
				<view class="">
					<view>问卷名称：{{item.title}}</view>
					<view>问卷轮次：{{item.round}}</view>
					<view>截止时间：{{$u.timeFormat(item.question_time, 'yyyy-mm-dd hh:MM:ss')}}</view>
				</view>
				<view class="question-enter">
					<u-button v-if="item.isDeadLine" type="primary" size="mini" plain :ripple="true">已结束</u-button>
					<u-button v-else type="primary" size="mini" plain :ripple="true" @click="itemClick(index)">回答</u-button>
				</view>
			</view>
			<u-empty v-if="questions.length==0" text="暂无问卷" mode="list"></u-empty>
		</view>

		<view style="height: 40rpx;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: 'https://cdn.uviewui.com/uview/common/logo.png',
				countArr: [],
				questions: [],
			};
		},
		created() {
			let that = this
			this.avatar = this.vuex_user.avatar_url
			// 监听从裁剪页发布的事件，获得裁剪结果
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: 'https://dc.huan-sha.net/api/upload/picture',
					filePath: path,
					name: 'picture',
					header: {
						'Authorization': "Bearer " + this.vuex_token
					},
					complete: (res, index) => {
						console.log(res);
						// 缓存修改好的图片
						this.$u.vuex('vuex_user', {
							...that.vuex_user,
							avatar_url: that.avatar
						});
						that.$u.api.updateUserInfo({
							id: this.vuex_user.id,
							avatar_url: that.avatar
						}).then(res => {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
						})
					}
				});
			})
		},
		onShow() {
			this.initData()
		},
		methods: {
			// 裁剪头像
			chooseAvatar() {
				this.$u.route({
					url: '/package_sub/pages/u-avatar-cropper/u-avatar-cropper',
					params: {
						// 输出图片宽度，高等于宽，单位px
						destWidth: 300,
						// 裁剪框宽度，高等于宽，单位px
						rectWidth: 200,
						// 输出的图片类型，如果'png'类型发现裁剪的图片太大，改成"jpg"即可
						fileType: 'jpg',
					}
				})
			},
			toMine() {
				uni.navigateTo({
					url: 'mine/mine'
				})
			},
			// 联系我们
			contactUs() {
				uni.makePhoneCall({
					phoneNumber: '15651519277'
				});
			},
			initData() {
				// 获取用户详情
				this.$u.api.getUserDeatil(this.vuex_user.id).then(res => {
					this.questions = res.data.Questions.filter(it => {
						return it.round > 0
					})
					this.questions=this.questions.map(it=>{
						let time1=new Date().getTime()
						let time2=new Date(it.question_time)
						return {...it,isDeadLine:time1>time2}
					})
				})
			},
			itemClick(index) {
				uni.navigateTo({
					url: `../question/answer/answer?index=${index}`
				})
			}
		},
	};
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background-color: #f4f6fa;
	}

	.question-list {
		margin: 60rpx 30rpx;
		padding: 20rpx;
		background-color: #fff;

		.question-title {
			text-align: center;
			margin-bottom: 10rpx;
			font-weight: 700;
			font-size: 30rpx;
		}

		.list-item {
			padding: 20rpx;
			background-color: rgba(215, 245, 255, .5);
			border-radius: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;


			.question-enter {
				margin-right: 4rpx;
			}
		}
	}

	.top {
		height: 250rpx;
		position: relative;

		.background {
			background-color: #5199ff;
			border-bottom-left-radius: 44rpx;
			border-bottom-right-radius: 44rpx;
			position: absolute;
			height: 180rpx;
			width: 100%;
		}
	}

	.icon {
		color: #96a1ae;
		font-size: 40rpx;
	}

	.user-card {
		height: 155rpx;
		padding: 0 30rpx;

		.card {
			position: relative;
			bottom: 124rpx;
			height: 250rpx;
			background-color: white;
			border-radius: 10rpx;

			.top {
				height: 30%;
				position: relative;

				.userImage {
					position: absolute;
					bottom: 24%;
					left: 10%;
					width: 150rpx;
					height: 150rpx;
					overflow: hidden;
					border-radius: 50%;
					// border: 4rpx solid white;
				}
			}

			.bottom {
				display: flex;
				height: 70%;

				.left {
					width: 80%;
					height: 100%;
					position: relative;

					.user-text {
						width: 100%;
						font-size: 1.6em;
						padding-left: 80rpx;
						height: 50%;
					}

					.user-phone {
						color: #96a1ae;
						padding-left: 80rpx;
						height: 50%;
						width: 100%;
						font-size: 0.9em;
					}
				}

				.right {
					width: 20%;
					height: 50%;
				}
			}
		}
	}

	.card-order {
		padding: 0 30rpx;
		margin-bottom: 30rpx;
	}

	.list-card {
		padding: 0 30rpx;

		.card {
			border-radius: 10rpx;
			position: relative;
			background-color: white;
			border-radius: 10rpx;
			padding: 10rpx 60rpx;

			.item {
				display: flex;
				height: 120rpx;

				.left {
					width: 15%;

					image {
						width: 70rpx;
						height: 70rpx;
					}
				}

				.center {
					width: 65%;
					display: flex;
					justify-content: start;
					align-items: center;
					font-size: 1.1em;
				}

				.right {
					width: 20%;
					justify-content: flex-end;
				}
			}
		}
	}

	.item-bottom-solid {
		border-bottom: 2rpx solid #d4d6da;
	}

	.quit {
		height: 100rpx;
		margin-top: 100rpx;

		.btn {
			background-color: #4f99ff;
			border-radius: 60rpx;
			width: 80%;
			color: white;
			font-size: 1.2em;
			height: 100%;
		}
	}

	.flex-center {
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
